package ua.khpi.si.kit49.diploma.db.dto;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * Class represents connections between cities
 * 
 * @author netstalk3r
 * 
 */

@Entity
@Table(name = "connections")
public class Connection {

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	private Integer id;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "id_start", nullable = false)
	private City cityStart;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "id_finish", nullable = false)
	private City cityFinish;

	@OneToOne(mappedBy = "connection", cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	private ConnectionFeature connectionFeature;

	public Connection() {
		super();
	}

	public Connection(City cityStart, City cityFinish,
			ConnectionFeature connectionFeature) {
		super();
		this.cityStart = cityStart;
		this.cityFinish = cityFinish;
		this.connectionFeature = connectionFeature;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public City getCityStart() {
		return cityStart;
	}

	public void setCityStart(City cityStart) {
		this.cityStart = cityStart;
	}

	public City getCityFinish() {
		return cityFinish;
	}

	public void setCityFinish(City cityFinish) {
		this.cityFinish = cityFinish;
	}

	public ConnectionFeature getConnectionFeature() {
		return connectionFeature;
	}

	public void setConnectionFeature(ConnectionFeature connectionFeature) {
		this.connectionFeature = connectionFeature;
	}

	@Override
	public String toString() {
		return "Connection: id - " + id + "; id_start - " + cityStart.getId()
				+ "; id_finish - " + cityFinish.getId();
	}

	@Override
	public int hashCode() {
		int hash = 29;
		hash = hash * 19 + ((id == null) ? 0 : id.hashCode());
		hash = hash * 19 + ((cityStart == null) ? 0 : cityStart.hashCode());
		hash = hash * 19 + ((cityFinish == null) ? 0 : cityFinish.hashCode());
		hash = hash
				* 19
				+ ((connectionFeature == null) ? 0 : connectionFeature
						.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Connection))
			return false;
		Connection entry = (Connection) obj;
		return id.equals(entry.getId())
				&& cityStart.equals(entry.getCityStart())
				&& cityFinish.equals(entry.getCityFinish())
				&& connectionFeature.equals(entry.getConnectionFeature());
	}
}
